

#include "iostream"
#include "vector"
#include "unordered_map"
#include "unordered_set"
#include "algorithm"
#include "string"
#include "list"
#include "string.h"

void to_line_end(){
    while(1){
        char a=std::cin.get();
        if(a=='\n'||a==EOF){
            return;
        }
    }
}
uint64_t get_char_v(char c){
    if(c>='0'&&c<='9'){
        return c-'0';
    }
    return 10+c-'A';
}
char get_num_c(int n){
    if(n>=0&&n<=9){
        return '0'+n;
    }
    return 'A'+(n-10);
}
class OneLoop {
public:
    uint64_t jinzhi=10;
    uint64_t num=0;
#define Add 0
#define Ass 1
#define Mul 2
#define Div 3
#define Mod 4
#define Sub 5

    int mode=Ass;
    void print_num(){
        std::list<int> nums;
        uint64_t ncpy=num;
        while (ncpy){
            nums.push_front(ncpy%jinzhi);
            ncpy/=jinzhi;
        }
        for(auto i:nums){
            printf("%c", get_num_c(i));
        }
        printf("\n");
    }
    //a数量 b种数
    bool run(int a
    ) {
        to_line_end();
        for(int i=0;i<a;i++){
            std::string s;
            char c;
            while(1){
                c=std::cin.get();
                if(c==' '||c=='\n'||c==EOF){
                    break;
                }
                s+=c;
            }

            if(s=="CLEAR"){
                num=0;
            }else if(s=="NUM"){
                uint64_t num1=0;
                while(1){
                    c=std::cin.get();
                    if(c=='\n'||c==EOF){
                        break;
                    }
                    num1*=jinzhi;
                    num1+= get_char_v(c);
                }

                switch (mode) {
                    case Ass:
                        num=num1;break;
                    case Add:
                        num+=num1;break;
                    case Div:
                        num/=num1;break;
                    case Mul:
                        num*=num1; break;
                    case Sub:
                        num-=num1;break;
                    case Mod:
                        num%=num1;break;
                }
            }else if(s=="CHANGE"){
                int cha;std::cin>>cha;
                this->jinzhi=cha;
                to_line_end();
            }else if(s=="ADD"){
                mode=Add;
            }else if(s=="MUL"){
                mode=Mul;
            }else if(s=="DIV"){
                mode=Div;
            }else if(s=="MOD"){
                mode=Mod;
            }else if(s=="EQUAL"){
                print_num();
            }else if(s=="SUB"){
                mode=Sub;
            }
            if(c=='\n'||EOF){
                continue;
            }
            to_line_end();
        }
        return true;
    }
};

class Solution {
public:
    void run() {

        bool first = true;
        int a, b, c, d, e;
        //        std::cin >> a;

        while (std::cin>>a) {

            OneLoop l;
            if (!l.run(a)) {
                //                return;
            }
        }


    }
};

int main() {
    //    bool cur_num= true;
    Solution().run();
    return 0;
}

